﻿/*
Copyright 2010 Andy Uhl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Reflection;

namespace ListMaker.App
{
	public partial class OptionsForm : Form
	{
		private const string RegistryNameValue = "ListMaker";
		private KeyEventArgs lastPressed;

		public OptionsForm()
		{
			InitializeComponent();
		}

		private MainForm MainForm
		{
			get
			{
				if (this.Owner is MainForm)
				{
					return (this.Owner as MainForm);
				}
				else
				{
					return null;
					// TODO: throw exception?
				}
			}
		}

		private void newShortcut_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Space)
			{
				lastPressed = null;
				newShortcut.Text = string.Empty;
			}
			else
			{
				lastPressed = e;

				newShortcut.Text = GetKeysText(e);

				if (!MainForm.IsKeyboardShortcutAvailable(e))
				{
					newShortcut.ForeColor = Color.Red;
				}
				else
				{
					newShortcut.ForeColor = SystemColors.WindowText;
				}
			}

			e.Handled = true;
			e.SuppressKeyPress = true;
		}

		private void ok_Click(object sender, EventArgs e)
		{
			ToggleRunOnStartup(startWithWindows.Checked);
			MainForm.KeyboardShortcut = lastPressed;
			MainForm.SetHotkeysEnabled(true);
			Close();
		}

		private static void ToggleRunOnStartup(bool run)
		{
			using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true))
			{
				if (run)
				{
					key.SetValue(RegistryNameValue, Application.ExecutablePath);
				}
				else
				{
					key.DeleteValue(RegistryNameValue, false);
				}
			}
		}

		private void OptionsForm_Activated(object sender, EventArgs e)
		{
			using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false))
			{
				startWithWindows.Checked = key.GetValueNames().Contains(RegistryNameValue);
			}

			currentShortcut.Text = GetKeysText(MainForm.KeyboardShortcut);
			MainForm.SetHotkeysEnabled(false);
		}

		public static string GetKeysText(KeyEventArgs keys)
		{
			if (keys == null)
			{
				return string.Empty;
			}

			StringBuilder sb = new StringBuilder();

			if (keys.KeyCode == Keys.LWin || keys.KeyCode == Keys.LWin)
				sb.Append("Win+");
			if (keys.Control)
				sb.Append("Ctrl+");
			if (keys.Alt)
				sb.Append("Alt+");
			if (keys.Shift)
				sb.Append("Shift+");

			char c = (char)keys.KeyValue;
			
			if (c > ' ')
				sb.Append(c);

			return sb.ToString();
		}

		private void cancel_Click(object sender, EventArgs e)
		{
			MainForm.SetHotkeysEnabled(true);
			Close();
		}
	}
}
